package com.peanut.baby.mvp;

import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.bumptech.glide.Glide;
import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.Photo;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.SysUtil;

import java.util.List;


/**
 * 关心的人症状ListView的数据适配器
 * Title: SymptomListAdapter
 * Company: Taetao
 * @date 2015年5月15日
 * @author ShaoZhuRui
 */
public class AddPhotoGridAdapter extends HolderAdapter<Photo,AddPhotoGridAdapter.PhotoHolder> {
//	private MyImageLoaderListener	mListener;
	private static final String TAG = AddPhotoGridAdapter.class.getSimpleName();

	private int 					mCellSize,mImageSize;

	private StateListDrawable 		mDrawable;

	private OnPhotoDeleteListener 	mRemoveListener;

	public AddPhotoGridAdapter(Context ctx, List<Photo> photos){
		super(ctx,photos);
		mCellSize = (InitManager.getInstance().getScreenWidth() - SysUtil.dip2px(55)) /5;
		mImageSize = mCellSize - SysUtil.dip2px(8);
		mDrawable = (StateListDrawable) ctx.getResources().getDrawable(R.drawable.selector_btn_add_photo);
		mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());


		Options options = new Options();
		options.inSampleSize = 10;
	}

	public void setOnPhotoDeleteListener(OnPhotoDeleteListener lis){
		this.mRemoveListener = lis;
	}


	@Override
	public View buildConvertView(LayoutInflater layoutInflater, Photo photo, int position) {
		return inflate(R.layout.grid_item_add_photo);
	}

	@Override
	public PhotoHolder buildHolder(View convertView, Photo goodInfo, int position) {
		PhotoHolder holder = new PhotoHolder();
		holder.mFrame = (FrameLayout) convertView.findViewById(R.id.attach_grid_frame);
		holder.mImage = (ImageView) convertView.findViewById(R.id.attach_grid_image);
		holder.mDelete = (ImageView) convertView.findViewById(R.id.attach_grid_delete);
		resizeViews(holder.mFrame, holder.mImage);

		return holder;
	}

	@Override
	public void bindViewDatas(PhotoHolder holder, final Photo photo, final int position) {
		if(!TextUtils.isEmpty(photo.url)){
			if(photo.url.equals(Photo.FLAG_ADD)){
				Glide.with(context)
                        .load(R.drawable.selector_btn_add_photo)
                        .error(R.drawable.selector_btn_add_photo)
                        .into(holder.mImage);
				holder.mDelete.setVisibility(View.GONE);
			}else{
				ImageLoader.getInstance().loadImageView(context,photo.url, holder.mImage);
				holder.mDelete.setVisibility(View.VISIBLE);
			}
		}else{
			ImageLoader.getInstance().loadImageView(context,photo.path, holder.mImage);
			holder.mDelete.setVisibility(View.VISIBLE);
		}

		holder.mDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if(mRemoveListener != null){
                    mRemoveListener.onPhotoDelete(position);

				}

			}
		});
	}


	class PhotoHolder{
		FrameLayout mFrame;
		ImageView mImage;
		ImageView mDelete;

	}




	
	private void resizeViews(FrameLayout frame,ImageView image){
		LayoutParams params = (LayoutParams) frame.getLayoutParams();
		params.width = mCellSize;
		params.height = mCellSize;
		frame.setLayoutParams(params);
		
		FrameLayout.LayoutParams _params = (FrameLayout.LayoutParams) image.getLayoutParams();
		_params.width = mImageSize;
		_params.height = mImageSize;
		_params.gravity = Gravity.BOTTOM;
		image.setLayoutParams(_params);
		
	}

	public interface OnPhotoDeleteListener{
		void onPhotoDelete(int position);
	}


	
}
